﻿using System;
using GNUGo.Net.CLI.CliControls;

namespace GNUGo.Net.CLI {
    public class CliGoBoardControl : CliControl {
        private GoGame _game;

        public GoGame Game {
            get { return _game; }
            set {
                if (_game != value) {
                    _game = value;
                    Width = 3 + (_game.Board.Size * 2) + 3;
                    Height = 1 + _game.Board.Size + 1;
                    Invalidate();
                }
            }
        }

        protected override void OnPaint(object sender, PaintEventArgs args) {
            base.OnPaint(sender, args);

            if (_game != null) {
                DrawBoardColumnLetters(args.Graphics);
                DrawBoardRowNumbers(args.Graphics);
                for (int x = 0; x < Game.Board.Size; x++) {
                    for (int y = 0; y < Game.Board.Size; y++) {
                        int screenX = Left + 3 + (x * 2);
                        int screenY = Top + 1 + (y);
                        switch (Game.Board[x, Game.Board.Size - 1 - y].State) {
                            case StoneState.Black:
                                args.Graphics.DrawString("X", ConsoleColor.Gray, ConsoleColor.Black, screenX, screenY);
                                break;
                            case StoneState.White:
                                args.Graphics.DrawString("O", ConsoleColor.Gray, ConsoleColor.Black, screenX, screenY);
                                break;
                            case StoneState.Empty:
                                if (Game.Board.IsStarPoint(x, Game.Board.Size - 1 - y)) {
                                    args.Graphics.DrawString("+", ConsoleColor.Gray, ConsoleColor.Black, screenX, screenY);
                                }
                                else {
                                    args.Graphics.DrawString(".", ConsoleColor.Gray, ConsoleColor.Black, screenX, screenY);
                                }
                                break;
                        }

                    }
                }
            }
        }

        private void DrawBoardRowNumbers(CliGraphics g) {
            Console.ForegroundColor = ConsoleColor.Gray;
            for (int i = 0; i < Game.Board.Size; i++) {
                int number = Game.Board.Size - i;
                g.DrawString(string.Format("{0,2}", number), ConsoleColor.Gray, ConsoleColor.Black, 0, i + 1);
                g.DrawString(string.Format("{0,2}", number), ConsoleColor.Gray, ConsoleColor.Black, 3 + (Game.Board.Size * 2), i + 1);
            }
        }

        private void DrawBoardColumnLetters(CliGraphics g) {
            Console.ForegroundColor = ConsoleColor.Gray;
            for (int i = 0; i < Game.Board.Size; i++) {
                char letter = Game.Board[i, 0].Letter;
                g.DrawCharacter(letter, ConsoleColor.Gray, ConsoleColor.Black, 3 + (i * 2), 0);
                g.DrawCharacter(letter, ConsoleColor.Gray, ConsoleColor.Black, 3 + (i * 2), 1 + Game.Board.Size);
            }
        }
    }
}
